package org.akomantoso.core.data;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.akomantoso.core.data.group.BlockElements;
import org.akomantoso.core.io.Input;
import org.akomantoso.core.io.Output;
import org.akomantoso.core.io.util.group.BlockElementsGroup;
import org.akomantoso.core.visitor.AKVisitor;

/**
 * The complex type itemType is similar to a hierarchical element, but is isolated and does not belong to a full
 * hierarchy.
 * 
 * <pre>
 * 	<xsd:complexType name="itemType">
 * 		<xsd:complexContent>
 * 			<xsd:extension base="basehierarchy">
 * 				<xsd:sequence minOccurs="1" maxOccurs="unbounded">
 * 					<xsd:group ref="blockElements" />
 * 				</xsd:sequence>
 * 				<xsd:attributeGroup ref="corereq" />
 * 			</xsd:extension>
 * 		</xsd:complexContent>
 * 	</xsd:complexType>
 * </pre>
 * 
 * @author <a href="mailto:jacques.militello@gmail.com">Jacques Militello</a>
 * 
 * @since http://www.akomantoso.org/1.0
 * 
 */
public abstract class ItemType extends BaseHierarchyCoreReq {

	// Mandatory (min 1)
	private final List<BlockElements> _blockElements = new ArrayList<BlockElements>();

	public List<BlockElements> getBlockElements() {
		return _blockElements;
	}

	/**
	 * @see org.akomantoso.core.data.BaseHierarchy#writeExternal(org.akomantoso.core.io.Output)
	 */
	@Override
	public <T extends Output> void writeExternal(T out) throws IOException {
		super.writeExternal(out);
		for (int i = 0, n = _blockElements.size(); i < n; i++) {
			_blockElements.get(i).writeExternal(out);
		}
	}

	/**
	 * @see org.akomantoso.core.data.BaseHierarchy#readExternal(org.akomantoso.core.io.Input)
	 */
	@Override
	public <T extends Input> void readExternal(T in) throws IOException {
		final String localName = in.getLocalName();
		super.readExternal(in);
		BlockElementsGroup.read(in, _blockElements, localName);
	}

	protected final void internalAccept(AKVisitor visitor) {
		super.internalAccept(visitor);
		for (int i = 0, n = _blockElements.size(); i < n; i++) {
			_blockElements.get(i).accept(visitor);
		}
	}
	
}